<template>
    <div>
          <van-nav-bar title="登录/注册" left-text="返回" left-arrow @click-left="back" />
          <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field
                v-model="username"
                name="用户名"
                label="用户名"
                placeholder="用户名"
                :rules="[{ required: true, message: '请填写用户名' }]"
                />
                <van-field
                v-model="password"
                type="password"
                name="密码"
                label="密码"
                placeholder="密码"
                :rules="[{ required: true, message: '请填写密码' }]"
                />

                <van-field
                center
                clearable
                label="验证码"
                placeholder="输入验证码"
                v-model="verify"
                >
                    <template #button>
                        <vue-img-verify ref="verifyRef" />
                    </template>
                </van-field>
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                提交
                </van-button>
            </div>
        </van-form>
    </div>
</template>

<script>
import { ref, reactive, toRefs } from 'vue';
import { useStore } from 'vuex';
import vueImgVerify from '/@//components/vueImgVerify.vue'
import { setLocal } from '/@//utils/storage.js'
import { useRouter } from 'vue-router'
import { Toast } from 'vant'

export default {
    name: "Login",
    components: {
        vueImgVerify
    },
    setup() {
        const verifyRef = ref(null)
        const state = reactive({
        username: '',
        password: '',
        username1: '',
        password1: '',
        type: 'login',
        imgCode: '',
        verify: ''
        })



    const router = useRouter()
    const store = useStore()

    const onSubmit = (values) => {
      console.log('submit', values);

      console.log('verifyRef.value.imgCode', verifyRef.value.imgCode)
      state.imgCode = verifyRef.value.imgCode || ''

      if (state.verify.toLowerCase() != state.imgCode.toLowerCase()) {
        Toast.fail('验证码有误')
        return
      }
      

     
      console.log('验证码通过');
     
        if(state.type == 'login') {
            let user = store.state.user
        //         // TODO login action
        //         // const store = useStore
        //         // const { data } = await store.dispatch('login') 
                setLocal("token", "hastoken")
        //         // 
            if( state.username == user.name && state.password == user.password){

                Toast.success({message: '登录成功', duration: 1000})
                router.push("/")
            }
        } else {
        //     // TODO 注册 action
            Toast.success('注册成功')
        //     state.type = 'login'
        //     state.verify = ''
        }


    }
    const back = () => router.go(-1)

    return {
        ...toRefs(state),
        onSubmit,
        back,
        verifyRef
        }

}}
</script>

<style lang="less">

</style>